package org.check.SQL;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import org.check.VO.DBConnVO;

public class ConnectionPool {  
    // ���ӳ���������  
    private int initSize;
    private boolean isActive = false; // ���ӳػ״̬  
    private int contActive = 0;// ��¼�������ܵ�������  
      
    // ��������  
    private List<Connection> freeConnection = new Vector<Connection>();  
    // �����  
    private List<Connection> activeConnection = new Vector<Connection>();
	private DBConnVO dbConnVO;  
	
  


 // ���̺߳����Ӱ󶨣���֤������ͳһִ�� 
    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>(); 


public ConnectionPool(DBConnVO dbConnVO) throws SQLException {
		this.dbConnVO = dbConnVO;
        init();  
        //cheackPool();  
    }  
  
    // ��ʼ��  
    public void init() throws SQLException {  
        for (int i = 0; i < Integer.parseInt(dbConnVO.getPoolCnt()); i++) {  
		    Connection conn;  
		    conn = ConnectionFactory.getConnectionByType(dbConnVO);
		    // ��ʼ����С������  
		    if (conn != null) {  
		        freeConnection.add(conn);  
		        contActive++;  
		    }  
		}  
		isActive = true;  
    }  
      
    // ��õ�ǰ����  
    public Connection getCurrentConnecton() throws SQLException{  
        // Ĭ���߳�����ȡ  
        Connection conn = threadLocal.get();  
        if(!isValid(conn)){  
            conn = getConnection();  
        }  
        return conn;  
    }  
  
    // �������  
    public Connection getConnection() throws SQLException {  
        Connection conn = null;  
        try {  
            // �ж��Ƿ񳬹��������������  
            if(contActive < 50){  
                if (freeConnection.size() > 0) {  
                    conn = freeConnection.get(0);  
                    if (conn != null) {  
                        threadLocal.set(conn); 
                        freeConnection.remove(0);  
                    }   
                }
                else {  
                    conn = ConnectionFactory.getConnectionByType(dbConnVO);  
                }  
                  
            }else{  
                // �����������,ֱ�����»������  
                wait(3000);  
                conn = getConnection();  
            }  
            if (isValid(conn)) {  
                activeConnection.add(conn);  
                contActive ++;  
            }  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        return conn;  
    }  
  
  
    // �ͷ�����  
    public synchronized void releaseConn(Connection conn) throws SQLException {  
        if (isValid(conn)&& !(freeConnection.size() > 50)) {  
            freeConnection.add(conn);  
            activeConnection.remove(conn);  
            contActive --;  
            threadLocal.remove();  
            // �������������ȴ����̣߳�ȥ������  
            //notifyAll();  
        }  
    }  
  
    // �ж������Ƿ����  
    private boolean isValid(Connection conn) {  
        try {  
            if (conn == null || conn.isClosed()) {  
                return false;  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return true;  
    }  
  
    // �������ӳ�  
    public synchronized void destroy() {  
        for (Connection conn : freeConnection) {  
            try {  
                if (isValid(conn)) {  
                    conn.close();  
                }  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
        for (Connection conn : activeConnection) {  
            try {  
                if (isValid(conn)) {  
                    conn.close();  
                }  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
        isActive = false;  
        contActive = 0;  
    }  
  
    // ���ӳ�״̬  
    
    public boolean isActive() {  
        return isActive;  
    }  
      
    // ��ʱ������ӳ����  
    
    /*public void cheackPool() {  
        if(dbBean.isCheakPool()){  
            new Timer().schedule(new TimerTask() {  
            @Override  
            public void run() {  
            // 1.���߳����������״̬  
            // 2.���ӳ���С ���������  
            // 3.����״̬���м�飬��Ϊ���ﻹ��Ҫд�����̹߳�����࣬��ʱ�Ͳ������  
            System.out.println("���߳���������"+freeConnection.size());  
            System.out.println("�����������"+activeConnection.size());  
            System.out.println("�ܵ���������"+contActive);  
                }  
            },dbBean.getLazyCheck(),dbBean.getPeriodCheck());  
        }  
    } */ 
}
